<?php

namespace App\Http\Controllers\Library;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Push extends Controller
{

    private static $uid;

    private static $type;

    /**
     * 推送时
     */
    private static $push_Url = "http://127.0.0.1:2121/";

    /**
     * 初始化项目
     */
    public function __construct(int $uid,$type = 'publish')
    {

        self::$uid = $uid;

        self::$type = $type;

    }

    /**
     * 发送推送
     * @return bool
     */
    public static function  PushCurl($text)
    {

        // 推送的url地址，使用自己的服务器地址
        $post_data = [
            "to"      => self::$uid, 
            "type"    => self::$type,
            "content" => $text,
        ];
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, self::$push_Url);
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        $output = curl_exec ( $ch );

        curl_close ( $ch );

        if($output != 'ok'){

            return false;
        }

        return true;
    }


}
